<?php
$shop_name = $this->getRequest()->getParam('shop_name');
$startDate = $this->getRequest()->getParam('start_date','');
$endDate = $this->getRequest()->getParam('end_date','');
$start_quote = $this->getRequest()->getParam('start_quote');
$end_quote = $this->getRequest()->getParam('end_quote');
$start_total = $this->getRequest()->getParam('start_total');
$end_total = $this->getRequest()->getParam('end_total');
$store_id = $this->getRequest()->getParam('store_id');

$orderIds = $this->getRequest()->getPost('order_ids');
$_coreHelper = $this->helper('core');
$_coreDate = Mage::getSingleton('core/date');

$session = Mage::getSingleton('admin/session');
$websiteId = $session->getUser()->getWebsiteId();

if(!$websiteId && $store_id)
{
	$websiteId = $store_id;
}

$teams = implode(',',$this->getTeamCustomer());

if($websiteId)
{
	$sql = "SELECT star,COUNT(star) AS total FROM zgb_store where website_id='{$websiteId}' and is_active=1 and entity_id not in ({$teams}) GROUP BY star ";
}
else
{
	$sql = "SELECT star,COUNT(star) AS total FROM zgb_store where is_active=1 and entity_id not in ({$teams}) GROUP BY star";
}

$resource = Mage::getSingleton('core/resource');
$db = $resource->getConnection('core_read');

$stars = $db->query($sql)->fetchAll();
$items = $this->getItemCollection();
?>

<div class="print">
	<div class="report-header">
		<h2>终端用户统计报表</h2>
		<div class="search-box">
			<form name="report_form" id="report_form" action="" method="get">
				<table class="data-table">
					<tr><th colspan="2">星级统计</th></tr>
					<?php foreach($stars as $star):?>
						<tr>
							<td><?php echo $star['star']?></td>
							<td><?php echo $star['total']?></td>
						</tr>
					<?php endforeach;?>
				</table>
				<table class="data-table">
					<tr>
						<td>
							下单日期:<input type="text" name="start_date" id="start_date" value="<?php echo $startDate;?>" class="laydate-icon"/> 至 <input type="text" name="end_date" id="end_date" value="<?php echo $endDate;?>" class="laydate-icon"/>
						</td>
						<td>
							店铺名:<input type="text" name="shop_name" id="shop_name" value="<?php echo $shop_name;?>"/>
						</td>
					</tr>

					<tr>
						<td>
							下单次数:<input type="text" name="start_quote" id="start_quote" value="<?php echo $start_quote;?>"/> 至 <input type="text" name="end_quote" id="end_quote" value="<?php echo $end_quote;?>"/>  
						</td>
						<td>
							下单金额:<input type="text" name="start_total" id="start_total" value="<?php echo $start_total;?>"/> 至 <input type="text" name="end_total" id="end_total" value="<?php echo $end_total;?>"/>
						</td>
					</tr>

					<?php if(!$session->getUser()->getWebsiteId()):?>
					<?php $stores = Mage::getModel('core/website')->getCollection();?>
					<tr>
						<td>区域:
							<select name="store_id">
								<option value="0">--请选择--</option>
								<?php foreach($stores as $store):?>
									<?php if($store->getId()==1) continue;?>
									<option value="<?php echo $store->getId(); ?>" <?php if($store->getId()==$store_id) echo 'selected';?>><?php echo $store->getName();?></option>
								<?php endforeach;?>
							</select>
						</td>
						<td></td>
					</tr>
					<?php endif;?>
					<tr>
						<td colspan="2">
							<input type="submit" value="统计" name="apply" id="apply"/> <input type="reset" value="重置" id="reset"/> <input type="submit" value="导出" name="export" id="export"/>
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	<script type="text/javascript">
		laydate({
		    elem: '#start_date',
		    format: 'YYYY-MM-DD',
		    event: 'focus'
		});
		laydate({
		    elem: '#end_date',
		    format: 'YYYY-MM-DD',
		    event: 'focus'
		});
	</script>
	<div class="order-print">
		<table class="data-table">
			<tr>
				<td>店铺名称</td>
				<td>区域</td>
				<td>注册日期</td>
				<td>下单次数</td>
				<!-- <td width="8%">下单数量</td> -->
				<td>订单金额</td>
				<td>首次下单</td>
				<td>最近下单</td>
				<td>未下单时间</td>
				<td>联系人</td>
				<td>联系电话</td>
				<td>备注</td>
			</tr>
			<?php foreach($items as $item):?>
			<?php 
				$orders = Mage::getModel('sales/order')
					->getCollection()
					->addFieldToSelect('created_at')
					->addFieldToFilter('customer_id', array('eq'=>$item['entity_id']))
					->setOrder('created_at','asc');
			?>
			<tr>
				<td><?php echo $item['store_name']; ?></td>
				<td><?php echo $item['website_name']; ?></td>
				<td><?php echo $this->convertDate($item['created_at']); ?></td>
				<td><?php echo $item['order_total'] ?></td>
				<!-- <td><?php echo $item['order_total']; ?></td> -->
				<td><?php echo $_coreHelper->formatPrice($item['grand_total'], false) ?></td>
				<td><?php echo $this->convertDate($orders->getFirstItem()->getCreatedAt()); ?></td>
				<td><?php echo $this->convertDate($orders->getLastItem()->getCreatedAt()); ?></td>
				<td><?php echo $this->aband($orders->getLastItem()->getCreatedAt()); ?></td>
				<td><?php echo $item['name']; ?></td>
				<td><?php echo $item['mobile']; ?></td>
				<td></td>
			</tr>
			<?php endforeach;?>
		</table>
	</div>
</div>
<script type="text/javascript">
(function($){
	// $("#apply").click(function(){
	// 	if($("#r_province_id").val())
	// 	{
	// 		if(!$("#r_city_id").val())
	// 		{
	// 			alert("请选择城市");
	// 			return;
	// 		}
	// 	}
	// 	$("#report_form").submit();
	// });
	// if($('#r_province_id').val()=='') 
 //    {
 //        $('#r_city_id').attr('disabled',true);
 //        $('#r_website_id').attr('disabled',true);
 //    }
	// $('#r_province_id').change(function(){      
 //        $.getJSON('<?php echo $this->getUrl('zgb/ajax/citys')?>?id='+$(this).val(), function(data){
 //            $('#r_city_id').empty();
 //            $('#r_website_id').empty();
 //            var option = $('<option>').val('').text('-请选择-');
 //            $('#r_website_id').append(option);
 //            for(var i=0; i<data.length;i++)
 //            {
 //                var option = $('<option>').val(data[i].value).text(data[i].label);
 //                $('#r_city_id').append(option);
 //            }
 //            $('#r_city_id').attr('disabled',false);
 //            $('#r_website_id').attr('disabled',true);
 //            $('#r_city_id').find('option:first').attr('selected',true);
 //        });
 //    });

 //    $("#r_city_id").change(function(){
 //        $('#r_website_id').attr('disabled',true);
 //        $.getJSON('<?php echo $this->getUrl('zgb/ajax/websites')?>?id='+$(this).val(), function(data){
 //            $('#r_website_id').empty();
 //            for(var i=0; i<data.length;i++)
 //            {
 //                var option = $('<option>').val(data[i].value).text(data[i].label);
 //                $('#r_website_id').append(option);
 //            }
 //            $('#r_website_id').attr('disabled',false);
 //            $('#r_website_id').find('option:first').attr('selected',true);
 //        });
 //    });


})(jQuery);
</script>